package com.wcansoft.baseWeb.mybatis_plugin.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.wcansoft.baseWeb.mybatis_plugin.dao.MybatisBaseDAO;
import com.wcansoft.baseWeb.mybatis_plugin.dao.Query;

public abstract class BaseService<T> {
	public abstract MybatisBaseDAO<T> getImpDao();
	public abstract Class<?> getClazz();
	
	public List<T> listPage(Query query,final int currpage,final int pagesize)
	{
		return getImpDao().listPage(getClazz(), query,currpage,pagesize);
	}
	
	//根据Query条件查记录
	public List<T> listQuery(Query query)
	{
		return getImpDao().listQuery(getClazz(), query);
	}
	

	//根据object赋值条件==查记录
	public List<T> list(T object){
		return getImpDao().list(object);
	}

	
	//根据主键值查一条记录
	public T get(Object id){
		return getImpDao().get(getClazz(),id);
	}
	//根据query条件查一条记录
	public T getOne(Query query){
		return getImpDao().getOne(getClazz(),query);
	}

	//根据query条件查记录数
	public Integer getRowCount(Query query){
		return getImpDao().getRowCount(getClazz(),query);
	}

	//根据query条件查一个字段
	public String getOneRowCol(Query query){
		return getImpDao().getOneRowCol(getClazz(),query);
	}

	//插入object赋值字段
	@Transactional
	public Integer insert(Object object){
		return getImpDao().insert(object);
	}
	@Transactional
	public Integer insertQuery(Query query){
		return getImpDao().insertQuery(query);
	}
	
	@Transactional
	public Integer insertList(List<T> list){
		 int i =0 ;
		for(T obj : list)
		{
			i  += getImpDao().insert(obj);
		}
		return i;
	}
	
	//根据主键，更新object赋值的字段
	@Transactional
	public Integer update(Object obj){
		return getImpDao().update(obj);
	}
	//保存对象，id为空insert,id不为空update
	@Transactional
	public Integer save(Object object){
		return getImpDao().save(object);
	}

	//根据条件，更新object赋值的字段
	@Transactional
	public Integer updateQuery(Object object,Query query){
		return getImpDao().updateQuery(object,query);
	}
	
	@Transactional
	public Integer updateQueryBySql(Query query){
		return getImpDao().updateQueryBySql(query);
	}

	//删除object赋值的字段
	@Transactional
	public Integer delete(Object object){
		return getImpDao().delete(object);
	}

	//删除Query查詢結果
	@Transactional
	public Integer deleteQuery(Query query){
		return getImpDao().deleteQuery(getClazz(),query);
	}
	
	//多个返回数据接口相同的Query对象合并查询
	public  static Query queryUnionAll(List<Query> list){
		Query ret=null;
		String sql="";
		int i=list.size();
		Object[] params=new Object[]{};
		for(int j=0;j<i;j++){
			Query query=list.get(j);
			if(j!=(i-1)){//表示最后一个
				sql+=query.getConditionValue().getCondition()+" union all ";
			}else{
				sql+=query.getConditionValue().getCondition();
			}
			
			params=concatAll(params, query.getConditionValue().getVals());
		}
		System.out.println(sql);
		ret=new Query(sql,params);
		return ret;
	}
	
	public static <T> T[] concatAll(T[] first, T[]... rest) {  
		  int totalLength = first.length;  
		  for (T[] array : rest) {  
		    totalLength += array.length;  
		  }  
		  T[] result = Arrays.copyOf(first, totalLength);  
		  int offset = first.length;  
		  for (T[] array : rest) {  
		    System.arraycopy(array, 0, result, offset, array.length);  
		    offset += array.length;  
		  }  
		  return result;  
		}
	

	
	
}
